package com.feicheng.service.a;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.exception.SystemException;
import com.feicheng.persistence.entity.Amd;
import com.feicheng.persistence.entity.Amj;
import com.feicheng.persistence.entity.Ayyjlitem;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.mapper.AmjMapper;
import com.feicheng.persistence.mapper.AyyjlitemMapper;
import com.feicheng.persistence.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class FMjService {

    @Autowired
    IAdzService dzService;

    @Autowired
    IClientService clientService;

    @Autowired
    IAmjService menjinService;

    @Autowired
    IAmdService iAmdService;

    @Autowired
    IAyyjlitemService iAyyjlitemService;

    @Autowired
    AyyjlitemMapper ayyjlitemMapper;

    public List<JSONObject> getYyclient(JSONObject param) {
        String mdid = param.getString("mdid");
        String fjid = param.getString("fjid");
        String rq = param.getString("rq");
        String st = param.getString("st");
        String et = param.getString("et");
        JSONArray status = param.getJSONArray("status");
        QueryWrapper<JSONObject> yyitqw = new QueryWrapper<>();

        yyitqw.eq("yyit.mdid", mdid);

        if (StringUtils.hasLength(fjid)) {
            yyitqw.eq("yyit.fjid", fjid);
        }

        if (StringUtils.hasLength(rq)) {
            yyitqw.eq("yyit.rq", rq);
        }

        if (StringUtils.hasLength(st)) {
            yyitqw.ge("yyit.st", st);
        }

        if (StringUtils.hasLength(et)) {
            yyitqw.le("yyit.et", et);
        }

        if (null != status && status.size() > 0) {
            yyitqw.in("yyit.statu", status);
        }

        yyitqw.isNotNull("cli.tx");

        List<JSONObject> yyjlitems = ayyjlitemMapper.getYyjlitemWithClient(yyitqw);

        return yyjlitems;
    }

    public JSONObject sqcode(JSONObject param) {
        String code = param.getString("code");
        String mjid = param.getString("mjid");

        if (!StringUtils.hasLength(code)) {
            throw new SystemException(code + "：code没有被找到");
        }

        if (!StringUtils.hasLength(mjid)) {
            throw new SystemException(mjid + "：sn没有被找到");
        }

        Amj menjin = menjinService.getById(mjid);

        String fjid = menjin.getFjid();
        String mdid = menjin.getMdid();
        LocalTime st = LocalTime.now().plusMinutes(5);
        LocalTime et = LocalTime.now().minusMinutes(5);


        if (code.startsWith("YYIT")) {
            QueryWrapper<Ayyjlitem> qw = new QueryWrapper<>();
            qw.eq("yyjlitemid", code);
            qw.eq("rq", LocalDate.now());

            if (StringUtils.hasLength(fjid)) {
                qw.eq("fjid", fjid);
            }

            qw.eq("mdid", mdid);
            qw.le("st", st);
            qw.ge("et", et);

            long count = iAyyjlitemService.count(qw);

            if (count > 0) {

            } else {

                /* 出门 */
                if (menjin.getFlg()) {
                    /* 进门 */
                } else {
                    throw new SystemException(code + "：当前没有预约；无权进入房间【"+fjid);
                }

            }
        } else if (code.startsWith("DZ")) {
            Amd amd = iAmdService.getById(mdid);
            if (!code.equals(amd.getDzid())) {
                throw new SystemException(code + "：不是同一家店；无权进入【"+mdid);
            }
        }

        return param;
    }

    public JSONObject sl(JSONObject param) {
        String clientid = param.getString("clientid");
        String mjid = param.getString("mjid");

        if (!StringUtils.hasLength(clientid)) {
            throw new SystemException(clientid + "：客户没有被找到");
        }

        if (!StringUtils.hasLength(mjid)) {
            throw new SystemException(mjid + "：门禁没有被找到");
        }
        Amj menjin = menjinService.getById(mjid);

        String fjid = menjin.getFjid();
        String mdid = menjin.getMdid();

        LocalTime st = LocalTime.now().plusMinutes(5);
        LocalTime et = LocalTime.now().minusMinutes(5);

        QueryWrapper<Ayyjlitem> qw = new QueryWrapper<>();
        qw.eq("clientid", clientid);
        qw.eq("rq", LocalDate.now());

        if (StringUtils.hasLength(fjid)) {
            qw.eq("fjid", fjid);
        }

        qw.eq("mdid", mdid);
        qw.le("st", st);
        qw.ge("et", et);

        long count = iAyyjlitemService.count(qw);

        if (count > 0) {

        } else {

            /* 出门 */
            if (menjin.getFlg()) {
                /* 进门 */
            } else {
                throw new SystemException(clientid + "：当前没有课程；无权进入房间【"+fjid);
            }

        }
        return param;
    }

    public JSONObject menjinHeartBeat(String mjid, Boolean statu) {
        JSONObject res = new JSONObject();
        Amj amenjin = new Amj();
        amenjin.setMjid(mjid);
        amenjin.setStatu(statu);
        menjinService.updateById(amenjin);
        return res;
    }


    /**
     * 新增门禁 成哥
     * @param param
     * @return
     */
    public JSONObject newMj(JSONObject param) {
        Amj amenjin = param.getObject("amenjin", Amj.class);

        if(!StringUtils.hasLength(amenjin.getMjid())){
            throw new SystemException("门店必须指定");
        }

        amenjin.setMjid(amenjin.getSn());
        amenjin.setSn(amenjin.getMjid());
        amenjin.setStatu(false);
        menjinService.save(amenjin);

        param.put("amenjin", amenjin);
        return param;
    }

    /**
     * 修改门禁 成哥
     * @param param
     * @return
     */
    public JSONObject updateMj(JSONObject param) {

        Amj amenjin = param.getObject("amenjin", Amj.class);
        menjinService.updateById(amenjin);
        return param;
    }

    /**
     * 删除门禁 成哥
     * @param param
     * @return
     */
    public JSONObject deleteMj(JSONObject param) {
        String mjid = param.getString("mjid");
        if (!StringUtils.hasLength(mjid)) {
            throw new SystemException("门禁必须指定");
        }
        menjinService.removeById(mjid);
        return param;
    }

    public JSONObject getMenjinBySn(String sn) {
        JSONObject param = new JSONObject();
        param.put("sn", sn);
        List<JSONObject> mjs = allMjByStoreAndRoom(param);

        if (mjs.size() > 0) {
            return mjs.remove(0);
        }
        return null;
    }

    /**
     * 取得所有门禁
     * @param param
     * @return
     */
    public List<JSONObject> allMjByStoreAndRoom(JSONObject param) {

        QueryWrapper<Amj> qw = new QueryWrapper<>();
        qw.eq("statu", true);

        String mdid = param.getString("mdid");
        if (StringUtils.hasLength(mdid)) {
            qw.eq("mdid", mdid);
        }

        String fjid = param.getString("fjid");

        if (StringUtils.hasLength(fjid)) {
            if ("null".equals(fjid)) {
                qw.isNull("fjid");
            } else {
                qw.eq("fjid", fjid);
            }
        }

        String sn = param.getString("sn");
        if (StringUtils.hasLength(sn)) {
            qw.eq("mjid", sn);
        }

        AmjMapper menjinMapper = (AmjMapper) menjinService.getBaseMapper();
        return menjinMapper.getMjWithSR(qw);
    }

    public List<Amj> allMj(JSONObject param) {

        QueryWrapper<Amj> qw = new QueryWrapper<>();
        qw.eq("statu", true);

        String mdid = param.getString("mdid");
        if (StringUtils.hasLength(mdid)) {
            qw.eq("mdid", mdid);
        }

        String fjid = param.getString("fjid");
        if (StringUtils.hasLength(fjid)) {
            if ("null".equals(fjid)) {
                qw.isNull("fjid");
            } else {
                qw.eq("fjid", fjid);
            }
        }

        String sn = param.getString("sn");
        if (StringUtils.hasLength(sn)) {
            qw.eq("mjid", sn);
        }

        return menjinService.list(qw);
    }


}
